<?php
// +----------------------------------------------------------------------
// | 跃飞 [ 将来的你一定会感激现在奋斗的自己 ]
// +----------------------------------------------------------------------
// | Author: 钟贵廷
// +----------------------------------------------------------------------
// | URI1: https://gitee.com/yeafy   URI2:https://github.com/gtzhong
// +----------------------------------------------------------------------
// | weChat:gt845272922  qq:845272922
// +----------------------------------------------------------------------

namespace app\yeafy\observer\demo1;


class Student implements ISubject
{
    private $observer = [];
    private $state;

    /**
     * 描述:添加观察者
     * @author 钟贵廷
     * @WeChat gt845272922
     * @qq 845272922
     * @param IObserver $observer
     */
    public function registerObserver(IObserver $observer)
    {
        $this->observer[] = $observer;
    }

    /**
     * 描述:删除观察者
     * @author 钟贵廷
     * @WeChat gt845272922
     * @qq 845272922
     * @param IObserver $observer
     */
    public function removeObserver(IObserver $observer)
    {
        foreach ($this->observer as $k=>$v)
        {
            if($v == $observer)
            {
                unset($this->observer[$k]);
            }
        }
    }

    public function notifyObserver()
    {
        foreach ($this->observer as $k=>$v)
        {
            $v->update($this->getState());
        }
    }

    /**
     * @return mixed
     */
    public function getState()
    {
        return $this->state;
    }

    /**
     * @param mixed $state
     */
    public function setState($state)
    {
        echo "同学正在$state<br/>";
        $this->state = $state;
        $this->notifyObserver();
    }




}